/*
reads in lights and remoted sensed DSI rasters and merge at annual level

Follows read_lights.R code to convert lights .tif files to ASCII 

precedes merge_grid.do to merge with all other data and created main data file
*also precedes merge_nocell.do to create subbasin and admin level data


*/


timer clear

log using read_lights_drought, replace
timer on 1
clear

* need to change directory for ras2dta, which won't accept filenames in quotes
cd "../data/grid_ascii"


forvalues yr = 2000(1)2011 { 

*droughts
ras2dta, file(drought_`yr') genxcoord(x) genycoord(y) dropmiss 
rename drought_`yr' drought
tempfile drought_`yr'
save `drought_`yr'', replace
clear


*ihs lights
ras2dta, file(lights_05deg_ihs_`yr') genxcoord(x) genycoord(y) 
rename lights_05deg_ihs_`yr' ihslights
tempfile lights_05deg_ihs_`yr'
save `lights_05deg_ihs_`yr'', replace
clear

*levels of lights
ras2dta, file(lights_05deg_`yr') genxcoord(x) genycoord(y) 
rename lights_05deg_`yr' lights

merge 1:1 x y using `drought_`yr'', keep(match)
drop _merge
merge 1:1 x y using `lights_05deg_ihs_`yr'', keep(match)
drop _merge

gen int year=`yr'

tempfile drought_lights_`yr'
save `drought_lights_`yr'', replace
clear
}


use `drought_lights_2000'
forvalues yr = 2001(1)2011 { 
append using `drought_lights_`yr''
}

sort x y year

cd ..

save grid_lights_drought, replace


timer list

log close
timer off 1

